<template>
  <div class="restaurant-list-page">
    <!-- 返回首页按钮 -->
    <div class="back-home">
      <el-button type="primary" @click="$router.push('/')" plain>
        <el-icon><House /></el-icon>
        返回首页
      </el-button>
    </div>

    <!-- 搜索区域 -->
    <div class="search-section">
      <div class="search-box">
        <el-input
          v-model="searchForm.name"
          placeholder="搜索餐厅名称"
          class="search-input"
          clearable
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <!-- 餐厅列表 -->
    <div class="restaurant-list" v-loading="loading">
      <el-card
        v-for="restaurant in restaurantList"
        :key="restaurant.id"
        class="restaurant-item"
        @click="handleRestaurantClick(restaurant)"
      >
        <el-image :src="restaurant.image" fit="cover" class="restaurant-img" />
        <div class="restaurant-info">
          <h3>{{ restaurant.name }}</h3>
          <p class="address">
            <el-icon><Location /></el-icon>
            {{ restaurant.address }}
          </p>
          <p class="desc">{{ restaurant.description }}</p>
          <div class="meta">
            <el-tag :type="restaurant.status === 1 ? 'success' : 'danger'" size="small">
              {{ restaurant.status === 1 ? '营业中' : '休息中' }}
            </el-tag>
            <span class="business-hours">{{ restaurant.businessHours }}</span>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="searchForm.page"
        v-model:page-size="searchForm.pageSize"
        :total="total"
        :page-sizes="[8, 16, 24, 32]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Search, Location, House } from '@element-plus/icons-vue'
import { getRestaurantList } from '@/api/restaurant'
import { ElMessage } from 'element-plus'

const router = useRouter()
const loading = ref(false)
const restaurantList = ref([])
const total = ref(0)

const searchForm = ref({
  page: 1,
  pageSize: 8,
  name: ''
})

// 获取餐厅列表
const fetchRestaurants = async () => {
  try {
    loading.value = true
    const res = await getRestaurantList(searchForm.value)
    restaurantList.value = res.data.records
    total.value = res.data.total
  } catch (error) {
    console.error('获取餐厅列表失败:', error)
    ElMessage.error('获取餐厅列表失败')
  } finally {
    loading.value = false
  }
}

// 处理搜索
const handleSearch = () => {
  searchForm.value.page = 1
  fetchRestaurants()
}

// 处理页码改变
const handleCurrentChange = (page) => {
  searchForm.value.page = page
  fetchRestaurants()
}

// 处理每页条数改变
const handleSizeChange = (size) => {
  searchForm.value.pageSize = size
  searchForm.value.page = 1
  fetchRestaurants()
}

// 点击餐厅卡片
const handleRestaurantClick = (restaurant) => {
  router.push(`/restaurant/${restaurant.id}`)
}

onMounted(() => {
  fetchRestaurants()
})
</script>

<style scoped lang="scss">
.restaurant-list-page {
  padding: 20px;
}

.back-home {
  margin-bottom: 20px;
}

.search-section {
  margin-bottom: 30px;
  display: flex;
  justify-content: center;

  .search-box {
    width: 600px;
    display: flex;
    gap: 15px;

    .search-input {
      flex: 1;
    }
  }
}

.restaurant-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 25px;
  margin-bottom: 30px;
}

.restaurant-item {
  cursor: pointer;
  transition: all 0.3s;
  border: none;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 20px rgba(0,0,0,0.15);
  }

  .restaurant-img {
    width: 100%;
    height: 250px;
    object-fit: cover;
  }

  .restaurant-info {
    padding: 20px;

    h3 {
      margin: 0 0 10px;
      font-size: 20px;
      font-weight: bold;
    }

    .address {
      display: flex;
      align-items: center;
      gap: 5px;
      margin: 0 0 10px;
      color: #666;
      font-size: 14px;
    }

    .desc {
      color: #666;
      margin: 0 0 15px;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
      line-height: 1.5;
    }

    .meta {
      display: flex;
      align-items: center;
      justify-content: space-between;

      .business-hours {
        color: #666;
        font-size: 14px;
      }
    }
  }
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}
</style>
